package com.woniuxy.cinemaServer.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cinemaServer.form.seatInfo.SeatLoadForm;
import com.woniuxy.cinemaServer.form.seatInfo.SeatChangeForm;
import com.woniuxy.cinemaServer.model.SeatInfo;
import com.woniuxy.cinemaServer.param.seatInfo.SeatChangeParam;
import com.woniuxy.cinemaServer.param.seatInfo.SeatLoadParam;
import com.woniuxy.cinemaServer.service.SeatInfoService;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.StateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.hutool.core.bean.BeanUtil.toBean;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年03月20日
 */
@RestController
@RequestMapping("/seatInfo")
@Api(tags = "座位接口")
public class SeatInfoController {
    @Autowired
    private SeatInfoService seatInfoService;

    @PostMapping("del/{id}.api")
    @ApiOperation("删除")
    public Result del(@PathVariable("id") Integer id){
        seatInfoService.del(id);
        return Result.getResult().setMsg("删除座位成功");
    }

    @PostMapping("change.api")
    @ApiOperation("修改")
    public Result change(@RequestBody SeatChangeForm form){
        SeatChangeParam param = toBean(form, SeatChangeParam.class);
        seatInfoService.change(param);
        return Result.getResult().setMsg("修改座位成功");
    }

    @PostMapping("/load")
    @ApiOperation("查询")
    public Result load(@RequestBody SeatLoadForm form){
        SeatLoadParam param = toBean(form, SeatLoadParam.class);
        List<SeatInfo> load = seatInfoService.load(param);
        return Result.getResult().setMsg("查询座位信息成功").setData(load);
    }

    @GetMapping("/loadSeatByCinemaId/{id}")
    @ApiOperation("查询")
    public Result<List<SeatInfo>> loadSeatByCinemaId(@PathVariable("id") Integer id){
        SeatLoadParam param = new SeatLoadParam();
        param.setCinemaId(id);
        param.setState(StateUtil.SEAT_STATE_NORMAL);
        List<SeatInfo> load = seatInfoService.load(param);
//        return Result.getResult().setMsg("查询座位信息成功").setData(pageInfo);
        System.out.println(load+"####");
        return Result.getListResult(SeatInfo.class).setMsg("查询座位信息成功").setData(load);
    }

    @PostMapping("/loadSeatByCinemaId.api")
    public Result<Map<Integer,List<SeatInfo>>> loadSeatByCinemaIds(@RequestBody List<Integer> ids) throws Exception{
        List<SeatInfo> seatInfos = seatInfoService.list(new QueryWrapper<SeatInfo>().in("cinema_id",ids).eq("seat_status",StateUtil.SEAT_STATE_NORMAL));
        Map<Integer, List<SeatInfo>> map = new HashMap<>();
        if (seatInfos!=null){
            seatInfos.forEach(seatInfo -> {
                List<SeatInfo> seatInfos1 = map.get(seatInfo.getCinemaId());
                if (seatInfos1==null) {
                    seatInfos1 = new ArrayList<>();
                    map.put(seatInfo.getCinemaId(),seatInfos1);
                }
                seatInfos1.add(seatInfo);
            });
        }
        return Result.getMapListResult(Integer.class,SeatInfo.class).setMsg("根据cinemaIds查询seats成功").setData(map);
    }
}

